// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

/*
 * System_String.cpp
 *
 *  Created on: Jan 6, 2010
 *      Author: dhovemey
 */

#include <cstdio>
#include <cassert>
#include <cstdint>
#include <string>
#include "interp/VM_Interpreter.h"
#include "interp/VM_Object.h"
#include "interp/VM_MagicMethod.h"

static void System_String_objCleanup(VM_Object *obj)
{
	std::wstring *rep = (std::wstring *) obj->getMagicPtr();
	//printf("cleaning up string %ls\n", rep->c_str());
	delete rep;
}

extern "C"
void System_String_magic(VM_Class *cls)
{
	cls->setObjectCleanupFunc(&System_String_objCleanup);
}

extern "C"
VM_Object* System_String_charAt(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	if (numArgs != 2 || !VM_INTERP->isType(VM_METHOD_ARG(1), CLASS_ID_INT32) || VM_METHOD_ARG(1)->getMagicInt() < 0) {
		// TODO: throw exception?
		return VM_INTERP->getNil();
	}

	std::wstring *str = (std::wstring *) VM_RECEIVER->getMagicPtr();

	if (std::string::size_type(VM_METHOD_ARG(1)->getMagicInt()) >= str->size()) {
		return VM_INTERP->getNil();
	}

	wchar_t c = (*str)[VM_METHOD_ARG(1)->getMagicInt()];
	return VM_INTERP->getVMCharacter(c, thread);
}

extern "C"
VM_Object* System_String_substring(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	if (numArgs < 2 || numArgs > 3) {
		// TODO: throw exception?
		return VM_INTERP->getNil();
	}

	VM_Object *begin = VM_METHOD_ARG(1);
	if (!VM_INTERP->isType(begin, CLASS_ID_INT32) || begin->getMagicInt() < 0) {
		// TODO: throw exception?
		return VM_INTERP->getInternedVMString(L"", thread);
	}

	std::wstring *str = (std::wstring *) VM_RECEIVER->getMagicPtr();

	if (numArgs == 2) {
		std::wstring sub = str->substr(std::wstring::size_type(begin->getMagicInt()));
		return VM_INTERP->getVMString(sub, thread);
	} else {
		VM_Object *end = VM_METHOD_ARG(2);
		if (!VM_INTERP->isType(end, CLASS_ID_INT32) || end->getMagicInt() < 0) {
			// TODO: throw exception?
			return VM_INTERP->getInternedVMString(L"", thread);
		}
		std::wstring sub = str->substr(
				std::wstring::size_type(begin->getMagicInt()),
				std::wstring::size_type(end->getMagicInt()));
		return VM_INTERP->getVMString(sub, thread);
	}
}

extern "C"
VM_Object* System_String_length(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	return VM_INTERP->getVMInt32(int32_t(((std::wstring*)VM_RECEIVER->getMagicPtr())->size()), thread);
}

extern "C"
VM_Object* System_String_equals(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	if (numArgs != 2) {
		// TODO: throw exception?
		return VM_INTERP->getVMBoolean(false);
	}
	VM_Object *other = VM_METHOD_ARG(1);
	if (!VM_INTERP->isType(other, CLASS_ID_STRING)) {
		return VM_INTERP->getVMBoolean(false);
	}

	if (VM_RECEIVER == other) {
		return VM_INTERP->getVMBoolean(true);
	}

	std::wstring *left = (std::wstring *) VM_RECEIVER->getMagicPtr();
	std::wstring *right = (std::wstring *) other->getMagicPtr();

	return VM_INTERP->getVMBoolean(*left == *right);
}

extern "C"
VM_Object* System_String_compareTo(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	if (numArgs != 2) {
		// TODO: throw exception?
		return VM_INTERP->getNil();
	}

	VM_Object *other = VM_METHOD_ARG(1);

	int cmp;
	if (!VM_INTERP->isType(other, CLASS_ID_STRING)) {
		// TODO: throw exception?
		cmp = VM_INTERP->compareDifferentTypes(VM_RECEIVER, other);
	} else {
		std::wstring *left = (std::wstring *) VM_RECEIVER->getMagicPtr();
		std::wstring *right = (std::wstring *) other->getMagicPtr();
		cmp = left->compare(*right);
	}

	return VM_INTERP->getVMInt32(int32_t(cmp), thread);
}

extern "C"
VM_Object* System_String_concat(VM_Thread *thread, VM_ThreadOperandStack::iterator argIter, unsigned numArgs)
{
	if (numArgs != 2) {
		// TODO: throw exception?
		return VM_INTERP->getNil();
	}

	VM_Object *other = VM_METHOD_ARG(1);

	if (!VM_INTERP->isType(other, CLASS_ID_STRING)) {
		// TODO: throw exception?
		return VM_INTERP->getNil();
	}

	std::wstring *left = (std::wstring *) VM_RECEIVER->getMagicPtr();
	std::wstring *right = (std::wstring *) other->getMagicPtr();

	std::wstring concatStr = *left + *right;

	return VM_INTERP->getVMString(concatStr, thread);
}
